package com.ciwong.epaper.ui;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import com.ciwong.libs.widget.CWToast;
import com.ciwong.mobilelib.utils.ToastUtil;
import com.ciwong.mobilelib.widget.DownloadProgressBar;
import com.ciwong.mobilelib.widget.NewCWProgressBar;

/**
 * Fragment基类
 *
 * @author sugy
 * @version ciwong v.1.0 2015/11/12 16:18
 * @since ciwong v.1.0
 */
public class BaseFragment extends Fragment {
    protected Activity mActivity;
    protected NewCWProgressBar progressBar;
    protected String url = "";

    /**
     * 显示全屏进度条
     */
    protected void showCricleProgress() {
        showCricleProgress(null);
    }

    /**
     * 显示全屏进度条
     *
     * @param msg
     */
    protected void showCricleProgress(String msg) {
        try {
            if (progressBar == null) {
                progressBar = new NewCWProgressBar(mActivity);
                progressBar.setCanceledOnTouchOutside(false);
                progressBar.setCancelable(true);
            }
            if (!mActivity.isFinishing()) {
                if (!TextUtils.isEmpty(msg)) {
                    progressBar.setMessage(msg);
                }
                if (!progressBar.isShowing()) {
                    progressBar.show();
                }
            }
        } catch (Exception e) {

        }


    }

    protected boolean isProgressBarShowing() {
        boolean isShowing = false;
        if (null != progressBar) {
            return progressBar.isShowing();
        }
        return isShowing;
    }

    /**
     * 显示全屏进度条
     *
     * @param resId
     */
    protected void showCricleProgress(int resId) {
        showCricleProgress(getString(resId));
    }


    /**
     * 关闭全屏进度条
     */
    protected void hideCricleProgress() {
        if (progressBar != null && !mActivity.isFinishing() && progressBar.isShowing()) {

            try {
                progressBar.dismiss();
            } catch (Exception e) {

            }
        }
    }

    /**
     * 吐丝显示错误信息(方块)
     *
     * @param resId 字符串资源id
     */
    protected void showToastError(int resId) {
        CWToast.makeText(mActivity, resId, Toast.LENGTH_SHORT, true)
                .setToastType(CWToast.TOAST_TYPE_ERROR).show();
    }

    /**
     * 吐丝显示错误信息(方块)
     *
     * @param msg 字符串
     */
    protected void showToastError(String msg) {
        CWToast.makeText(mActivity, msg, Toast.LENGTH_SHORT, true)
                .setToastType(CWToast.TOAST_TYPE_ERROR).show();
    }

    /**
     * 吐丝显示错误信息(方块)
     *
     * @param errorCode 字符串
     */
    protected void showToastError(int errorCode, Object data) {
        if (errorCode != 17 && errorCode != 27) {
            CWToast.makeText(mActivity, String.valueOf(data), Toast.LENGTH_LONG, true)
                    .setToastType(CWToast.TOAST_TYPE_ERROR).show();
        }

    }

    /**
     * 吐丝显示成功信息(方块)
     *
     * @param resId 字符串资源id
     */
    protected void showToastSuccess(int resId) {
        ToastUtil.INSTANCE.toastCenterSuccess(resId);
    }

    /**
     * 吐丝显示成功信息(方块)
     *
     * @param msg 字符串
     */
    protected void showToastSuccess(String msg) {
        ToastUtil.INSTANCE.toastCenterSuccess(msg);

    }

    /**
     * 吐丝显示提示信息(方块)
     *
     * @param resId 字符串资源id
     */
    protected void showToastAlert(int resId) {
        CWToast.makeText(mActivity, resId, Toast.LENGTH_SHORT, true)
                .setToastType(CWToast.TOAST_TYPE_ALERT).show();
    }

    /**
     * 吐丝显示提示信息(方块)
     *
     * @param msg 字符串
     */
    protected void showToastAlert(String msg) {
        CWToast.makeText(mActivity, msg, Toast.LENGTH_SHORT, true)
                .setToastType(CWToast.TOAST_TYPE_ALERT).show();
    }

    /**
     * 长方形系统默认的style吐司，居中显示。
     *
     * @param msg
     */
    protected void toastInCenter(String msg) {
        Toast toast = Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    /**
     * 长方形系统默认的style吐司，居中显示。
     *
     * @param
     */
    protected void toastInCenter(int resId) {
        ToastUtil.INSTANCE.toastCenterError(resId);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mActivity = activity;
    }


    public void showDownloadProgress() {
        showDownloadProgress("");
    }


    public DownloadProgressBar mDownloadProgressBar;

    /**
     * 显示下载全屏进度条
     */
    public void showDownloadProgress(String msg) {

        if (mDownloadProgressBar == null) {
            mDownloadProgressBar = new DownloadProgressBar(mActivity);
            mDownloadProgressBar.setCanceledOnTouchOutside(false);
            mDownloadProgressBar.setCancelable(true);
        }
        if (!mActivity.isFinishing()) {
            if (!TextUtils.isEmpty(msg)) {
                mDownloadProgressBar.setMessage(msg);
            }
            if (!isDownloadProgressBarShowing()) {
                mDownloadProgressBar.setCancelable(false);
                mDownloadProgressBar.hideProgressBar();
                mDownloadProgressBar.show();
            }
        }


        if (null != mDownloadProgressOnDismissListener) {
            mDownloadProgressBar.setOnDownLoadDialogDismissListener(mDownloadProgressOnDismissListener);
        }

    }


    /**
     * 关闭下载全屏进度条
     */
    public void hideDownloadProgress() {
        if (mDownloadProgressBar != null && !mActivity.isFinishing() && isDownloadProgressBarShowing()) {
            mDownloadProgressBar.dismiss();
        }
        //取消按钮默认不显示
        hideCancelButtonOfDownloadProgress();
    }

    /**
     * 取消按钮隐藏
     */
    public void hideCancelButtonOfDownloadProgress() {
        try {
            if (mDownloadProgressBar != null) {
                mDownloadProgressBar.getCancelButton().setVisibility(View.GONE);
                mDownloadProgressBar.setCancelable(false);
                mDownloadProgressBar.hideProgressBar();
            }
        } catch (Exception e) {

        }

    }

    /**
     * 取消按钮显示
     */
    public void showCancelButtonOfDownloadProgress() {
        if (mDownloadProgressBar != null && isDownloadProgressBarShowing()) {
            mDownloadProgressBar.getCancelButton().setVisibility(View.VISIBLE);
            mDownloadProgressBar.setCancelable(true);
        }
    }

    public boolean isDownloadProgressBarShowing() {
        if (mDownloadProgressBar != null) {
            return mDownloadProgressBar.isShowing();
        }
        return false;
    }

    DownloadProgressBar.OnDownLoadDialogDismissListener mDownloadProgressOnDismissListener;

    public void setDownloadProgressOnDismissListener(DownloadProgressBar.OnDownLoadDialogDismissListener onDismissListener) {
        mDownloadProgressOnDismissListener = onDismissListener;
    }

    public void updateDownloadProgress(int progress) {
        if (mDownloadProgressBar != null && !mActivity.isFinishing()) {
            mDownloadProgressBar.updateDownloadProgress(progress);
        }
    }

    public void hideDownloadHorizontalProgressBar() {
        if (mDownloadProgressBar != null && !mActivity.isFinishing()) {
            mDownloadProgressBar.hideProgressBar();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}
